﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Story.Library
{
    /// <summary>
    /// 提供web访问的方法
    /// </summary>
    public class WebHelper
    {
        public CookieContainer cookies = new CookieContainer();

        /// <summary>
        /// 发送Post 请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">发送的数据</param>
        /// <param name="referer">标头值</param>
        /// <returns></returns>
        public WebResponse doPost(Uri url, string postData, string referer)
        {
            try
            {


                byte[] paramByte = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Timeout = 1000 * 60 * 60;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Referer = referer;
                webRequest.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)";
                webRequest.ContentLength = paramByte.Length;
                webRequest.CookieContainer = this.cookies;

                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(paramByte, 0, paramByte.Length);    //写入参数
                newStream.Close();
                return webRequest.GetResponse();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 发送Post 请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">发送的数据</param>
        /// <returns></returns>
        public WebResponse doPost(Uri url, string postData)
        {
            try
            {


                byte[] paramByte = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Timeout = 1000 * 60 * 60;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)";
                webRequest.ContentLength = paramByte.Length;
                webRequest.CookieContainer = this.cookies;
                
                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(paramByte, 0, paramByte.Length);    //写入参数
                newStream.Close();
                return webRequest.GetResponse();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 发送Get请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public WebResponse doGet(Uri url)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Timeout = 1000 * 60 * 60;
                webRequest.CookieContainer = this.cookies;
                webRequest.Method = "GET";
                return webRequest.GetResponse();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 发送Get请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="referer">标头值</param>
        /// <returns></returns>
        public WebResponse doGet(Uri url, string referer)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Timeout = 1000 * 60 * 60;
                webRequest.CookieContainer = this.cookies;
                webRequest.Referer = referer;
                webRequest.Method = "GET";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)";
                return webRequest.GetResponse();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 根据请求返回相应字符串
        /// 编码为程序自动识别
        /// </summary>
        /// <param name="webresponse">请求</param>
        /// <returns></returns>
        public string ResponseToString(WebResponse webresponse)
        {
            try
            {
                EncodHelper ehelper = new EncodHelper();
                Encoding encoding = Encoding.GetEncoding(ehelper.GetEncodingName(webresponse.GetResponseStream()));

                using (StreamReader streamR = new StreamReader(webresponse.GetResponseStream(), encoding))
                {
                    string code = streamR.ReadToEnd();
                    webresponse.Close();
                    streamR.Close();
                    return code;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 根据请求和编码，返回相应的字符串
        /// </summary>
        /// <param name="webresponse">请求</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public string ResponseToString(WebResponse webresponse, Encoding encoding)
        {
            try
            {
                using (StreamReader streamR = new StreamReader(webresponse.GetResponseStream(), encoding))
                {
                    string code = streamR.ReadToEnd();
                    webresponse.Close();
                    streamR.Close();
                    return code;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
